package assemblers.beans;

import assemblers.interfaces.AddressInfoAssembler;
import assemblers.interfaces.PersonInfoAssembler;
import assemblers.interfaces.PhoneNumberInfoAssembler;
import infos.AddressInfo;
import infos.PersonInfo;
import infos.PhoneNumberInfo;
import model.Person;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;

@Stateless(name = "PersonInfoAssembler")
public class PersonInfoAssemblerBean implements PersonInfoAssembler {

    private AddressInfoAssembler addressInfoAssembler;
    private PhoneNumberInfoAssembler phoneNumberInfoAssembler;

    @EJB
    public void setAddressInfoAssembler(AddressInfoAssembler addressInfoAssembler) {
        this.addressInfoAssembler = addressInfoAssembler;
    }

    @EJB
    public void setPhoneNumberInfoAssembler(PhoneNumberInfoAssembler phoneNumberInfoAssembler) {
        this.phoneNumberInfoAssembler = phoneNumberInfoAssembler;
    }

    @Override
    public PersonInfo assemble(Person person) {
        PersonInfo info = new PersonInfo();
        info.setDescription(person.getDescription());
        info.setFirstName(person.getFirstName());
        info.setId(person.getId());
        info.setLastName(person.getLastName());
        return info;
    }

    @Override
    public PersonInfo assembleFull(Person person) {
        PersonInfo personInfo = assemble(person);
        List<AddressInfo> addressInfos = addressInfoAssembler.assembleList(person.getAddresses());
        List<PhoneNumberInfo>  phoneNumbers = phoneNumberInfoAssembler.assembleList(person.getPhoneNumbers());
        personInfo.setAddresses(addressInfos);
        personInfo.setPhoneNumbers(phoneNumbers);
        return personInfo;
    }

    @Override
    public List<PersonInfo> assembleList(List<Person> persons) {
        List<PersonInfo> personInfos = new ArrayList<PersonInfo>();
        for (Person p : persons) {
            PersonInfo info = assemble(p);
            personInfos.add(info);
        }
        return personInfos;
    }
}
